package com.sctvcloud.zhaohua_sd.ui.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.gridsum.tracker.GridsumWebDissector;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.DateUtils;
import com.ruihang.generalibrary.utils.ListUtils;
import com.ruihang.generalibrary.utils.ServerTime;
import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.beans.FChannel;
import com.sctvcloud.zhaohua_sd.beans.FLiveProgram;
import com.sctvcloud.zhaohua_sd.beans.FProgram;
import com.sctvcloud.zhaohua_sd.beans.TvLivingList;
import com.sctvcloud.zhaohua_sd.http.AbsNetCallBack;
import com.sctvcloud.zhaohua_sd.http.NetUtils;
import com.sctvcloud.zhaohua_sd.ui.adapter.RadioLivingAdapter;
import com.sctvcloud.zhaohua_sd.ui.adapter.TvLivingAdapter;
import com.sctvcloud.zhaohua_sd.ui.util.RBaseItemDecoration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 广播电台页面
 */
public class RadioFragment extends ColumnBaseFragment implements CanRefreshLayout.OnRefreshListener {
    private final int WHAT_REFRESH_TIME = 919;
    @BindView(R.id.tv_living_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView living_list;
    private RadioLivingAdapter livingAdapter;
    private LinearLayoutManager manager;
    private boolean mIsLoading = false;
    private Handler handler;
    private List<FChannel> fPrograms = new ArrayList<>();
    private FChannel currentChannel;
    private TvFragment parentFragment;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg != null) {
                    handMsg(msg);
                }
            }
        };
    }

    public void setParentFragment(TvFragment parentFragment) {
        this.parentFragment = parentFragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_radio, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        if (handler != null) {
            handler.removeMessages(WHAT_REFRESH_TIME);
        }
        super.onDestroyView();
    }

    @Override
    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setLoadMoreEnabled(false);
        manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        living_list.setItemAnimator(new DefaultItemAnimator());
        living_list.addItemDecoration(new RBaseItemDecoration(2));
        living_list.setLayoutManager(manager);
        initData();

    }

    @Override
    protected boolean onFragmentShow(boolean isFirst, boolean isCallFromResume) {
        if (currentChannel != null&&null!=parentFragment) {
                parentFragment.setPlayMedia(currentChannel, true);
        }
        return super.onFragmentShow(isFirst, isCallFromResume);
    }

    @Override
    public void onRefresh() {
        if (mIsLoading) {
            return;
        }
        mIsLoading = true;
//        if (TextUtils.isEmpty(((TvFragment) getParentFragment()).getProgramBackUrl())) {
//            ((TvFragment) getParentFragment()).setLoadingState(true);
//        }
    }

    private void handMsg(Message msg) {
        if (msg.what == WHAT_REFRESH_TIME) {
            handler.removeMessages(WHAT_REFRESH_TIME);
        }
    }

    public void setData(final List<FChannel> fChannels) {
        if(null!=fPrograms){
            fPrograms.clear();
        }
        this.fPrograms = fChannels;
//        initData();
    }

    private synchronized void onNetFinish() {
        mIsLoading = false;
        refreshLayout.refreshComplete();
    }


    private void initData() {
        if (livingAdapter == null) {
            livingAdapter = new RadioLivingAdapter(getActivity(), fPrograms).setItemInternalClick(livingOnItemClick);
            living_list.setAdapter(livingAdapter);
        } else {
            JLog.e("getTvData=fPrograms+"+fPrograms.size());
            livingAdapter.setData(fPrograms);
            livingAdapter.notifyDataSetChanged();
        }

    }

    int oldPos = -1;
    private OnItemInternalClick livingOnItemClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            FChannel fProgram = livingAdapter.getItem(position);
            currentChannel = fProgram;
            if(null!=parentFragment) {
                parentFragment.setPlayMedia(fProgram,true);
            }
            livingAdapter.setSelectPos(position);
            livingAdapter.notifyDataSetChanged();
            ArrayList<Pair<String, String>> pairs = new ArrayList<>();
            pairs.add(new Pair<>("页面", "首页"));
            pairs.add(new Pair<>("栏目", "频道"));
            pairs.add(new Pair<>("位置", "广播电台"));
            pairs.add(new Pair<>("行为类型", "点击"));
            GridsumWebDissector.getInstance().trackEvent(RadioFragment.this, "", fProgram.getChannelName(), "", 200, pairs);
        }
    };

    private void makeToast(int strRes) {
//        if (((TvFragment) getParentFragment()).getIndexInViewPager() != ((HomeFragment) getParentFragment().getParentFragment())
//                .getCurrentSelectedIndex()) {
//            return;
//        }
        // toast(R.string.loading_no_data);
    }


}
